//
//  FMDBSCDManager.m
//  NursingManage
//
//  Created by Zhen Huang on 17/5/15.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "FMDBSCDManager.h"
#import "FMDatabaseAdditions.h"
#import "HSCDModel.h"
#import <TMCache.h>
static FMDBSCDManager *manager = nil;

@implementation FMDBSCDManager

//实现单例方法
+(FMDBSCDManager *)sharedFMDBManeger {
    if (manager == nil) {
        manager = [[FMDBSCDManager alloc]init];
    }
    return manager;
}

//准备好文件路径
- (NSString *)backFilePath {
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    NSString *newFilePath = [documentPath stringByAppendingPathComponent:@"HSCDList.sqlite"];
    return newFilePath;//将新文件路径返回；
}

//创建数据库
- (void)dataBase {
    //创建数据库对象
    self.db = [FMDatabase databaseWithPath:[self backFilePath]];
    //打开数据库
    if ([self.db open]) {
        NSLog(@"数据库打开成功");
    }else {
        NSLog(@"数据库打开失败");
    }
}

//创建表
- (void)createTable {
    [self.db open];
    //1.准备sql语句
    NSString *sqlString = @"create table if not exists 'HSCDList'('scd_group_id'text,'emp_id'text,'scd_date'text,'scd_class'text,'class_name'text)";
    //2.执行语句
    BOOL result = [self.db executeUpdate:sqlString];
    NSLog(result ? @"创建成功":@"创建失败");
    
    [self.db close];
    
}

//添加数据
- (void)insertSCD:(HSCDModel *)MailModel {
    [self.db open];
    BOOL result = [self.db executeUpdate:@"insert into  HSCDList (scd_group_id,emp_id,scd_date,scd_class,class_name)values (?,?,?,?,?)",MailModel.SCD_GROUP_ID,MailModel.EMP_ID,MailModel.SCD_DATE,MailModel.SCD_CLASS,MailModel.CLASS_NAME];
    NSLog(result ? @"添加成功" :@"添加失败");
    [self.db close];
}

//查询数据 相当于数据读取操作
- (NSMutableArray *)allModel {
    //执行查询语句，得到查询结果
    NSString *emp_id = [[TMCache sharedCache]objectForKey:@"emp_id"];
    NSString *sqlStr = [NSString stringWithFormat:@"select * from HSCDList where emp_id='%@'",emp_id];
    FMResultSet *set = [self.db executeQuery:sqlStr];
    //上述方法就是将数据去全部获取放进集合
    NSMutableArray *array = [NSMutableArray array];
    while ([set next]) {
        HSCDModel *model = [[HSCDModel alloc]init];
        model.SCD_CLASS = [set stringForColumn:@"scd_class"];
        model.SCD_DATE = [set stringForColumn:@"scd_date"];
        model.SCD_GROUP_ID = [set stringForColumn:@"scd_group_id"];
        model.CLASS_NAME = [set stringForColumn:@"class_name"];
        model.EMP_ID = [set stringForColumn:@"emp_id"];
        [array addObject:model];
        
    }
    [self.db close];
    return array;
}


//删除数据
- (void)deleteSCD:(HSCDModel *)MailModel {
    [self.db open];
    BOOL result = [self.db executeUpdate:@"delete from HSCDList"];
    NSLog(result ? @"删除成功":@"删除失败");
    
    [self.db close];
}

////更新数据
//- (void)updateDog:(HMailListModel *)MailModel {
//    [self.db open];
//    //1.准备sql语句
//
//    BOOL result = [self.db executeUpdate:@"update mailList set emp_id = ?,jub_num = ?,input_code1 = ?,where number = ?",aDog.name,aDog.gender,aDog.type,aDog.number];
//    NSLog(result ? @"更新成功":@"更新失败");
//    [self.db close];
//
//}
//


@end
